package com.example.demo.service.impl;

import com.example.demo.common.entity.JsonResult;
import com.example.demo.common.entity.JsonResultStatus;
import com.example.demo.common.service.redis.RedisService;
import com.example.demo.dao.UserMapper;
import com.example.demo.entity.User;
import com.example.demo.entity.UserExample;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Mojo on 2017/8/30.
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private RedisService redisService;

	@Override
	public User getUser(String username) throws Exception{
		Object user = redisService.get(username);
		if(user != null){
			return (User)user;
		}
		UserExample userExample = new UserExample();
		userExample.createCriteria().andUsernameEqualTo(username);
		List<User> users = userMapper.selectByExample(userExample);
		if(users != null && users.size() > 0){
			return users.get(0);
		}else{
			logger.info("未查到相关用户数据");
			return null;
		}
	}

	@Override
	@Transactional(readOnly = false,propagation = Propagation.REQUIRED,rollbackFor={Exception.class, RuntimeException.class})
	public JsonResult addUser(User user) throws Exception{
		JsonResult jsonResult = new JsonResult();
		UserExample userExample = new UserExample();
		userExample.createCriteria().andUsernameEqualTo(user.getUsername());
		List<User> users = userMapper.selectByExample(userExample);
		if(users != null && users.size() > 0){
			jsonResult.setRet(JsonResultStatus.RET_FAIL);
			jsonResult.setMsg("该用户已存在");
		}else{
			user.setPassword(UUID.randomUUID().toString().replace("-",""));
			user.setCreateTime(new Date());
			userMapper.insert(user);
			try{
				redisService.set(user.getUsername(),user);
			}catch (Exception e){
				logger.error("addUser 接口缓存异常",e);
			}
		}
		return jsonResult;
	}

	@Override
	public JsonResult getAllUser(int pageNum, int pageSize) throws Exception {
		JsonResult jsonResult = new JsonResult();

		UserExample userExample = new UserExample();
		userExample.createCriteria();

		//分页处理
		PageHelper.startPage(pageNum,pageSize);
		/**
		 * 在你需要进行分页的Mybatis方法前调用PageHelper.startPage静态方法即可，
		 * 紧跟在这个方法后的第一个Mybatis查询方法会被进行分页。
		 * 后面的不会被分页，除非再次调用PageHelper.startPage
		 */
		List<User> users = userMapper.selectByExample(userExample);
		PageInfo<User> pageInfo = new PageInfo<User>(users);

		jsonResult.setPageNum(pageInfo.getPageNum());
		jsonResult.setPageSize(pageInfo.getSize());
		jsonResult.setTotal(pageInfo.getTotal());
		jsonResult.setData(users);
		return jsonResult;
	}

}
